//
//  ZHFindRentMsgFiltrateViewController.m
//  sc-cyan-ios
//
//  Created by lj on 2018/1/5.
//  Copyright © 2018年 greenbuilding. All rights reserved.
//

#import "ZHFindRentMsgFiltrateViewController.h"

#import "UICollectionViewLeftAlignedLayout.h"
#import "ZHProductScreeningCell.h"
#import "ZHProductScreeningHeaderView.h"

#import "ZHProductScreening.h"
#import "ZHProductScreeningChild.h"

static NSString *const cellID = @"FindRentMsgFiltrate";
static NSString *const headerID = @"FindRentMsgFiltrateHeader";


@interface ZHFindRentMsgFiltrateViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

//数据
@property (nonatomic,strong) NSMutableArray *dataList;

@end

@implementation ZHFindRentMsgFiltrateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpView];
}

- (NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
        
        NSString *str = @"傻逼了吧";
        
        NSMutableArray *child = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
            
            NSString *name = [str substringFromIndex:arc4random()%str.length];
            NSDictionary *dict = @{@"name":name,@"index":@"1"};
            [child addObject:dict];
        }
        
        NSDictionary *dic = @{@"title":@"产品分类",@"child":child};
        
        NSArray *data = @[dic,dic,dic,dic,dic];
        
        _dataList = [ZHProductScreening mj_objectArrayWithKeyValuesArray:data];
        
    }
    return _dataList;
    
}

- (void)setUpView{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
    
    _collectionView.collectionViewLayout = layout;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    }
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZHProductScreeningCell class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:cellID];
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZHProductScreeningHeaderView class]) bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
    

    //设置默认选中状态
    [self resetAction];
    
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataList.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    ZHProductScreening *screening = self.dataList[section];
    return screening.child.count;
}

- (CGSize)collectionView:(UICollectionView *)uiCollectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 不等宽
    ZHProductScreening *screening = self.dataList[indexPath.section];
    screening.supW = Main_Screen_Width - 50;
    ZHProductScreeningChild *child = screening.child[indexPath.item];
    child.supW = Main_Screen_Width - 50;
    
    return CGSizeMake(child.itemW, 30);
    
    //等宽
    //    return CGSizeMake(screening.sectionItemW, 30);
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZHProductScreeningCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    
    ZHProductScreening *screening = self.dataList[indexPath.section];
    
    cell.model = screening.child[indexPath.item];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

//定义每个Section的四边间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);//分别为上、左、下、右
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(Main_Screen_Width, 40);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView =nil;
    if (kind == UICollectionElementKindSectionHeader) {
        ZHProductScreeningHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID forIndexPath:indexPath];
        
        return headerView;
    }
    return reusableView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //设置选中状态
    ZHProductScreening *model = self.dataList[indexPath.section];
    ZHProductScreeningChild *selChild = model.child[indexPath.item];
    
    for (ZHProductScreeningChild *child in model.child) {
        child.isSel = NO;
    }
    selChild.isSel = YES;
    
    [self.collectionView reloadData];
}

#pragma mark ====== 事件触发区域========
//重置
- (IBAction)resetAction {
    for (int i = 0; i<self.dataList.count; i++) {
        //默认选中第0个
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:i];
        [self collectionView:_collectionView didSelectItemAtIndexPath:indexPath];
    }
}

- (IBAction)completeAction {
    
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}


@end
